Ext.onReady(function() {


    // 获取车辆列表
    var carList_store = new Ext.data.Store({
        storeId: 'store3',
        proxy: new Ext.data.HttpProxy({
            url: 'Get_Car_List.action'
        }),
        reader: new Ext.data.JsonReader({
            fields: [{
                    name: 'name'
                }, {
                    name: 'value'
                }]
        })
    });

    //获取驾驶员列表
    var driverList_store = new Ext.data.Store({
        storeId: 'store4',
        autoLoad: true,
        proxy: new Ext.data.HttpProxy({
            url: 'Get_Data_List.action?method=driver'
        }),
        reader: new Ext.data.JsonReader({
            fields: [
                {
                    name: 'name'
                }, {
                    name: 'value'
                }
            ]
        })
    });

    carList_store.load();
    var maintain = new Ext.FormPanel({
        labelAlign: 'maintain',
        frame: true,
        bodyStyle: 'padding:5px 5px 0',
        width: 600,
        items: [{
                layout: 'column',
                items: [{
                        columnWidth: .5,
                        layout: 'form',
                        items: [{
                                xtype: 'combo',
                                fieldLabel: '车牌号码',
                                name: 'car_code',
                                displayField: 'name',
                                valueField: 'name',
                                emptyText: '车牌号码',
                                anchor: '95%',
                                typeAhead: true,
                                triggerAction: 'all',
                                queryParam: 'name', autoSelect: false, triggerAction : 'query', triggerAction : 'all', store: carList_store
                            }, {
                                xtype: 'datefield',
                                fieldLabel: '保养日期',
                                emptyText: '保养日期（必填）',
                                allowBlank: false,
                                altFormats: 'Ymd',
                                format: 'Y-m-d',
                                dateFormat: 'Ymd',
                                name: 'maintain_date',
                                anchor: '95%'
                            }, {
                                xtype: 'textfield',
                                fieldLabel: '保养价格',
                                emptyText: '保养价格（必填）',
                                allowBlank: false,
                                name: 'price',
                                anchor: '95%'
                            }]
                    }, {
                        columnWidth: .5,
                        layout: 'form',
                        items: [{
                                xtype: 'textfield',
                                fieldLabel: '保养里程',
                                emptyText: '保养里程（必填）',
                                allowBlank: false,
                                name: 'mileage',
                                anchor: '95%'
                            }, {
                                xtype: 'combo',
                                fieldLabel: '保养人',
                                name: 'name',
                                displayField: 'name',
                                valueField: 'name',
                                emptyText: '保养人',
                                anchor: '95%',
                                typeAhead: true,
                                queryParam: 'name', autoSelect: false, triggerAction : 'all', store: driverList_store
                            }]
                    }]
            }, {
                xtype: 'htmleditor',
                id: 'remark',
                name: 'remark',
                fieldLabel: '保养内容',
                //height:200,
                anchor: '98%'
            }]
    });

    var win = new Ext.Window({
        id: 'win',
        title: "添加车辆保养记录",
        border: false,
        bodyBorder: false,
        // layout:'fit',
        width: 600,
        closeAction: 'hide',
        modal: true,
        tbar: ['-', {
                text: ' 提交并继续',
                handler: function() {
                    onSubmit();
                }
            }, '-', {
                text: '重置',
                handler: function() {
                    maintain.getForm().reset();
                }
            }, '-'],
        items: [maintain]
    });

    function onSubmit() {
        // 提交表单
        if (maintain.getForm().isValid()) {
            var submitValues = maintain.getForm().getValues();
            for (var param in submitValues) {
                if (maintain.form.findField(param)
                        && maintain.form.findField(param).emptyText == submitValues[param]) {
                    maintain.form.findField(param).setValue(' ');
                }
            }
            maintain.getForm().submit({
                url: 'MaintainAction.action',
                waitMsg: '数据保存中...',
                submitEmptyText: false,
                success: function(c, d) {
                    Ext.MessageBox.alert("操作信息", "成功信息保存！");
                    maintain.getForm().reset();
                },
                failure: function(c, d) {
                    Ext.MessageBox.show({
                        title: "操作信息",
                        msg: "信息保存出错，请联系管理员！",
                        buttons: Ext.MessageBox.OK,
                        icon: "ext-mb-error"
                    });
                }
            });
        }
    }

    win.show();
});
